#!/bin/bash
GNAPVERSION="1.0"

if [ "$1" == "-h" -o "$1" == "--help" ]; then
  echo "$0 [-s stage2file] [snapshot_stamp [version_stamp [stage]...]]"
  echo
  echo "Options:"
  echo "      -s stagefile:   File to use as a Stage2 seed tarball"
  echo "                      (Mandatory for complete builds or stage3 builds)"
  echo "      snapshot_stamp: Portage snapshot to use timestamp"
  echo "                      (by default, the current date)"
  echo "      version_stamp:  GNAP timestamp"
  echo "                      (by default, same as Portage snapshot timestamp)"
  echo "      stage:          Execute the given build stage."
  echo "   	              Accepted values: snapshot stage3 livecd-stage1"
  echo "                                        livecd-stage2 tarball"
  echo "                      (by default, execute all stages, bypassing the"
  echo "                      snapshot stage if a snapshot_stamp was provided)"
  echo
  echo "Examples:"
  echo "      $0 -s stage2-x86-uclibc-hardened-2004.3.tar.bz2"
  echo "      Do a complete GNAP rebuild using the official 2004.3 stage2 seed"
  echo
  echo "      $0 20041020 20041105 livecd-stage1 livecd-stage2"
  echo "      Build a livecd iso, starting from an already built stage3-20041105"
  echo "      tarball, using an existing 20041020 Portage snapshot"
  exit
fi

MAINDIR=`pwd`
if [ ! -f $MAINDIR/specs/common.conf ]; then
  NAME=`basename $0`
  echo $NAME must be run directly from the GNAP directory : ./$NAME $1 ...
  exit
fi

source gnapcatalyst.conf
source specs/common.conf
export CHOST
export CFLAGS
export CXXFLAGS

if [ "$1" == "-s" ]; then
  shift
  STAGE2FILE=$1
  shift
fi

if [ $# -eq 0 ]; then
  SNAPSHOTSTAMP=`date +%Y%m%d`
  SNAPSHOT=1  
else
  SNAPSHOTSTAMP=$1
  shift
fi

if [ $# -eq 0 ]; then
  STAMP=$SNAPSHOTSTAMP
else
  STAMP=$1
  shift
fi

ALLPARAMS=$*
if [ $# -eq 0 ]; then
  ALLPARAMS="stage3 livecd-stage1 livecd-stage2 tarball"
fi

for PARAM in $ALLPARAMS ; do
  if [ $PARAM == snapshot ]; then
    SNAPSHOT=1
  fi
  if [ $PARAM == stage3 ]; then
    STAGE3=1
  fi
  if [ $PARAM == livecd-stage1 ]; then
    LIVECD1=1
  fi
  if [ $PARAM == livecd-stage2 ]; then
    LIVECD2=1
  fi
  if [ $PARAM == tarball ]; then
    TARBALL=1
  fi
done

if [ "$STAGE3" == "1" ]; then
  if [ "$STAGE2FILE" == "" ]; then
    echo "]]] Please use \"-s\" option to define seed stage file [[["
    echo "]]] Use --help option for help                       [[["
    exit
  fi
fi

# Snapshot phase
if [ "$SNAPSHOT" == "1" ]; then
  echo "[[[[[[[[[[[[[[[[[[[[[[[[[[ SNAPSHOT ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]"
  catalyst -c gnapcatalyst.conf -C \
    target=snapshot \
    version_stamp=$SNAPSHOTSTAMP \
    portdir_overlay=$MAINDIR/specs/portage_overlay
  
  if [ $? -ne 0 ]; then
    echo "]]]]]]]]]]]]]]]]] snapshot failed ! [[[[[[[[[[[[[[[["
    exit
  fi
fi

if [ "$STAGE3" == "1" ]; then
  echo "[[[[[[[[[[[[[[[[[[[[[[[[[[ STAGE3 ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]"
  cp -v $STAGE2FILE $storedir/builds/$RELTYPE/seedstage.tar.bz2 
  catalyst -c gnapcatalyst.conf -C \
    target=stage3 \
    subarch=$SUBARCH \
    rel_type=$RELTYPE \
    snapshot=$SNAPSHOTSTAMP \
    version_stamp=$STAMP \
    profile=$PROFILE \
    source_subpath=$RELTYPE/seedstage \
    portage_confdir=$MAINDIR/specs/portage_confdir

  if [ $? -ne 0 ]; then
    echo "]]]]]]]]]]]]]]]]] stage3 failed ! [[[[[[[[[[[[[[[["
    exit
  else
    rm $storedir/builds/$RELTYPE/seedstage.tar.bz2
  fi
fi

if [ "$LIVECD1" == "1" ]; then
  echo "[[[[[[[[[[[[[[[[[[[[[[[[ LIVECD-STAGE1 ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]"
  TEMPCONF=`mktemp`;
  if [ $? != 0 ]; then
    echo "Failed to make tmpfile"
    exit 1
  fi
  cat >> $TEMPCONF <<EOF
target: livecd-stage1
subarch: $SUBARCH
rel_type: $RELTYPE
snapshot: $SNAPSHOTSTAMP
version_stamp: $STAMP
profile: $PROFILE
source_subpath: $RELTYPE/stage3-$SUBARCH-$STAMP
portage_confdir: $MAINDIR/specs/portage_confdir
EOF
  cat specs/packages.conf >> $TEMPCONF

  catalyst -c gnapcatalyst.conf -f $TEMPCONF
  rm $TEMPCONF

  if [ $? -ne 0 ]; then
    echo "]]]]]]]]]]]]]]]]] livecd-stage1 failed ! [[[[[[[[[[[[[[[["
    exit
  fi
fi

if [ "$LIVECD2" == 1 ]; then
  echo "[[[[[[[[[[[[[[[[[[[[[[[[ LIVECD-STAGE2 ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]"
  TEMPCONF=`mktemp`;
  if [ $? != 0 ]; then
    echo "Failed to make tmpfile"
    exit 1
  fi
  cat >> $TEMPCONF <<EOF
target: livecd-stage2
subarch: $SUBARCH
rel_type: $RELTYPE
snapshot: $SNAPSHOTSTAMP
version_stamp: $STAMP
profile: $PROFILE
source_subpath: $RELTYPE/livecd-stage1-$SUBARCH-$STAMP
boot/kernel/gentoo/sources: =$KERNEL_SOURCES
boot/kernel/gentoo/config: $MAINDIR/specs/kernel.config
boot/kernel/gentoo/extraversion: GNAP-$GNAPVERSION
livecd/iso: gnap-$GNAPVERSION-$STAMP.iso
livecd/fsscript: $MAINDIR/specs/fsscript
EOF
  cat specs/livecd.conf >> $TEMPCONF

  catalyst -c gnapcatalyst.conf -f $TEMPCONF
  rm $TEMPCONF

  if [ $? -ne 0 ]; then
    echo "]]]]]]]]]]]]]]]]] livecd-stage2 failed ! [[[[[[[[[[[[[[[["
    exit
  fi
fi

if [ "$TARBALL" == 1 ]; then
  echo "[[[[[[[[[[[[[[[[[[[[[[[[ TARBALL ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]"

  if [ ! -e gnap-$GNAPVERSION-$STAMP.iso ]; then
    echo "]]] No gnap-$GNAPVERSION-$STAMP.iso file to convert to tarball ! [[["
    exit
  fi

  if [ -e gnap-$GNAPVERSION-$STAMP.tar ]; then
    echo "]]]]]] gnap-$GNAPVERSION-$STAMP.tar already exists ! [[[[[["
    exit
  fi

  TEMPMNTDIR=`mktemp -d -t gnapmnt_XXXXXXXXXX`
  if [ $? != 0 ]; then
    echo "Failed to make tmpfile"
    exit 1
  fi
  TEMPISODIR=`mktemp -d -t gnapiso_XXXXXXXXXX`
  if [ $? != 0 ]; then
    echo "Failed to make tmpfile"
    exit 1
  fi
  mount -o loop gnap-$GNAPVERSION-$STAMP.iso $TEMPMNTDIR
  cp -r $TEMPMNTDIR/* $TEMPISODIR
  umount $TEMPMNTDIR
  rm -rf $TEMPMNTDIR
  cp specs/isolinux/isolinux.cfg $TEMPISODIR/isolinux/
  cp specs/isolinux/syslinux.cfg $TEMPISODIR/
  cp specs/isolinux/boot.msg $TEMPISODIR/isolinux/
  DATE=`date --utc`
  echo "GNAP-$GNAPVERSION-$STAMP built on $DATE" \
    >> $TEMPISODIR/isolinux/boot.msg

  tar cf gnap-$GNAPVERSION-$STAMP.tar -C $TEMPISODIR .
  rm -rf $TEMPISODIR
  
fi
echo "[[[[[[[[[[[[[[[[[[[[[[[[ DONE ! ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]"
